// src/api/role.ts

// src/types/role.ts

export interface TRoleEntity {
  reId?: number;
  roleName: string;
  remark?: string;
  createTime?: Date;
  updateTime?: Date;
}

export interface TRoutingEntity {
  rtId?: number;
  rtName: string;
  rtPath: string;
  parentId?: number;
  // 其他字段...
}

export interface TRoleAuthorizationEntity {
  raId?: number;
  roleId: number;
  routingId: number;
}

export interface RoleDetailResponse {
  role: TRoleEntity;
  permissions: number[]; // 权限 ID 列表
}

export interface AccessMenuResult {
  success: boolean;
  code: number;
  message: string;
  data: {
    data: TRoleEntity[]; // 注意：这里嵌套了两层 data
  };
}


import { baseRequestClient, requestClient } from '#/api/request';

/**
 * 查询所有角色
 */
export async function findAllRoleApi() {
  const res = await requestClient.get<AccessMenuResult[]>('/role/findAll');
  return res;
}

/**
 * 查询全部权限路由
 */
export async function findAllPurviewApi() {
  const res = await requestClient.get<TRoutingEntity[]>('/role/purview');
  return res;
}

/**
 * 分页查询角色列表
 * @param params 查询参数
 */
export async function getRoleListApi(params: Record<string, any>) {
  const res = await requestClient.get<{ page: any }>('/role/list', { params });
  return res;
}

/**
 * 获取角色详情及权限
 * @param reId 角色ID
 */
export async function getRoleInfoApi(reId: number) {
  const res = await requestClient.get<RoleDetailResponse>(`/role/info/${reId}`);
  return res;
}

/**
 * 新增角色
 * @param role 角色数据
 */
export async function addRoleApi(role: Partial<TRoleEntity>) {
  const res = await requestClient.post('/role/save', role);
  return res;
}

/**
 * 更新角色
 * @param role 角色数据
 */
export async function updateRoleApi(role: Partial<TRoleEntity>) {
  const res = await requestClient.post('/role/update', role);
  return res;
}

/**
 * 批量更新权限
 * @param authList 权限列表
 */
export async function updateRolePermissionsApi(authList: TRoleAuthorizationEntity[]) {
  const res = await requestClient.post('/role/updateBs', authList);
  return res;
}

/**
 * 删除角色
 * @param reIds 角色ID数组
 */
export async function deleteRoleApi(reIds: number[]) {
  const res = await requestClient.post('/role/delete', reIds);
  return res;
}

/**
 * 新增或更新角色（通用）
 * @param role 角色数据
 */
export async function saveOrUpdateRoleApi(role: Partial<TRoleEntity>) {
  const res = await requestClient.post('/role/saveOrUpdate', role);
  return res;
}
